
package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapeer;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    ;
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapeer setmealDishMapeer;

    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存了dish属性中数据
        this.save(dishDto);
        //保存菜品口味到菜品dishFlavor
        //获取菜品id
        Long dishId = dishDto.getId();
        //口味集合
        List<DishFlavor> flavors = dishDto.getFlavors();
        //
 /*       flavors=flavors.stream().map(dishFlavor -> {
            dishFlavor.setDishId(dishId);
            return dishFlavor;
            }).collect(Collectors.toList());*/
        // dishFlavorService.saveBat 吗r更新上传id数据

        if (flavors != null && flavors.size() > 0) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }
        }
    }

    /*
     * 通过 菜品名称id
     * */
    @Override
    public DishDto updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        Long dishId = dishDto.getId();
        //通过id
        Dish dish = this.getById(dishId);
        BeanUtils.copyProperties(dish, dishDto);
        //通过id查询口味数据
        LambdaQueryWrapper<DishFlavor> dishFlavorLwq = new LambdaQueryWrapper<>();
        dishFlavorLwq.eq(DishFlavor::getDishId, dishId);
        //调用业务层通过id先删除口味数据
        dishFlavorService.remove(dishFlavorLwq);
        //获取口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dishId);
            dishFlavorService.save(dishFlavor);
        }
        return dishDto;
    }

    /*
     *   删除菜品
     * */
    @Autowired
    SetmealDishService setmealDishService;

    @Transactional
    public R delete(List<Long> ids) {
      /*  LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
        dishLqw.in(Dish::getId, ids);
        dishLqw.eq(Dish::getStatus, 0);
        //获取菜品集合
        List<Dish> dishList = this.list(dishLqw);
        //判断售卖状态不能删除
        if (dishList.size() != ids.size()) {
            return R.error("有商品售卖状态不能删除");
        }
        for (Dish dish : dishList) {
            Long dishId = dish.getId();
            //判断菜品是否关联套餐
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getDishId, dishId);
            int count = setmealDishService.count(lqw);
            if (count > 0) {
                //套餐关联菜品不能删除
                return R.error("套餐关联菜品不能删除");
            }
            //没有关联 可以删除 菜品
            boolean remove = this.remove(dishLqw);
            //删除口味
            LambdaQueryWrapper<DishFlavor> dishFlavorLqw = new LambdaQueryWrapper<>();
            dishFlavorLqw.in(DishFlavor::getDishId, ids);
            dishFlavorService.remove(dishFlavorLqw);
        }
        return R.success("删除成功");*/

        LambdaQueryWrapper<Dish> dishLqw = new LambdaQueryWrapper<>();
        dishLqw.in(Dish::getId, ids);
        dishLqw.eq(Dish::getStatus, 1);
        int count = this.count(dishLqw);
        if (count > 0) {
            //有售卖状态不能删除
            return R.error("有售卖状态不能删除");
        }
        //判断菜品是否关联套餐
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getDishId, ids);
        count = setmealDishService.count(lqw);
        if (count > 0) {
            //套餐关联菜品不能删除
            return R.error("套餐关联菜品不能删除");
        }
        //没有关联可以删除
        this.removeByIds(ids);
        //删除菜品关联口味
        LambdaQueryWrapper<DishFlavor> dishFlavor = new LambdaQueryWrapper<>();
        dishFlavor.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavor);
        return R.success("删除成功");
    }
}


